package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.ml.doctor.R;
import com.ml.doctor.bean.TXBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;
import com.ml.doctor.view.BasicEditItem;
import com.ml.doctor.view.XRadioGroup;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TiXianActivity extends BaseActivity {
    @BindView(R.id.rb_zfb)
    RadioButton rbZfb;
    @BindView(R.id.rb_yhk)
    RadioButton rbYhk;
    @BindView(R.id.rg)
    XRadioGroup rg;
    @BindView(R.id.bi_zfb_name)
    BasicEditItem biZfbName;
    @BindView(R.id.bi_zfb_zh)
    BasicEditItem biZfbZh;
    @BindView(R.id.bi_zfb_jine)
    BasicEditItem biZfbJine;
    @BindView(R.id.layout_zfb)
    LinearLayout layoutZfb;
    @BindView(R.id.bi_yl_name)
    BasicEditItem biYlName;
    @BindView(R.id.bi_yl_zh)
    BasicEditItem biYlZh;
    @BindView(R.id.bi_yl_yh)
    BasicEditItem biYlYh;
    @BindView(R.id.bi_yl_jine)
    BasicEditItem biYlJine;
    @BindView(R.id.layout_yl)
    LinearLayout layoutYl;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    double gold;
    double amount;
    String remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        ButterKnife.bind(this);
        gold = getDoubleParam("gold");
        setTopTitle("提现");
        setRightText("提现明细");
        ketixian();
        rg.setOnCheckedChangeListener(new XRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(XRadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_zfb:
                        layoutZfb.setVisibility(View.VISIBLE);
                        layoutYl.setVisibility(View.GONE);
                        break;
                    case R.id.rb_yhk:
                        layoutZfb.setVisibility(View.GONE);
                        layoutYl.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rbZfb.isChecked()) {
                    if (TextUtils.isEmpty(biZfbName.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "名字不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    if (TextUtils.isEmpty(biZfbZh.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "账号不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    if (TextUtils.isEmpty(biZfbJine.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "金额不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    try {
                        amount = Double.parseDouble(biZfbJine.getEditString());
                        remark = "支付宝," + biZfbName.getEditString() + "," + biZfbZh.getEditString();
                        getFwNum();
                    } catch (Exception e) {
                        ToastUtil.show(TiXianActivity.this, "请输入正确的金额", Toast.LENGTH_SHORT);
                        return;
                    }
                }
                if (rbYhk.isChecked()) {
                    if (TextUtils.isEmpty(biYlName.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "名字不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    if (TextUtils.isEmpty(biYlZh.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "账号不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    if (TextUtils.isEmpty(biYlYh.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "银行不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    if (TextUtils.isEmpty(biYlName.getEditString())) {
                        ToastUtil.show(TiXianActivity.this, "金额不能为空", Toast.LENGTH_SHORT);
                        return;
                    }
                    try {
                        amount = Double.parseDouble(biYlJine.getEditString());
                        remark = "银行卡," + biYlName.getEditString() + "," + biYlZh.getEditString() + "," + biYlYh.getEditString();
                        getFwNum();
                    } catch (Exception e) {
                        ToastUtil.show(TiXianActivity.this, "请输入正确的金额", Toast.LENGTH_SHORT);
                        return;
                    }
                }
            }
        });
    }

    public void getFwNum() {
        showLoadingDialog("提现中");
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/docter_service_count", map, Double.class, new NetworkManager.SuccessCallback<Double>() {
            @Override
            public void onSuccess(Double response) {
                getDaiSh();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                hideLoadingDialog();
                ToastUtil.showShort(TiXianActivity.this, message);
            }
        });
    }

    public void getDaiSh() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserNick() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "record/daitixian_amount", map, Double.class, new NetworkManager.SuccessCallback<Double>() {
            @Override
            public void onSuccess(Double response) {
                commit();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                hideLoadingDialog();
                ToastUtil.showShort(TiXianActivity.this, message);
            }
        });
    }

    public void commit() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        map.put("amount", amount + "");
        map.put("remark", remark + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/app_update_docter_amount", map, Double.class, new NetworkManager.SuccessCallback<Double>() {
            @Override
            public void onSuccess(Double response) {
                hideLoadingDialog();
                ToastUtil.show(TiXianActivity.this, "审核已提交", Toast.LENGTH_SHORT);
                onRightTextClick();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                hideLoadingDialog();
                ToastUtil.showShort(TiXianActivity.this, message);
            }
        });
    }

    public void ketixian() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "record/keyong_amount", map, TXBean.class, new NetworkManager.SuccessCallback<TXBean>() {
            @Override
            public void onSuccess(TXBean response) {
                biZfbJine.getEditText().setHint("可提现" + response.amount);
                biYlJine.getEditText().setHint("可提现" + response.amount);
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(TiXianActivity.this, message);
            }
        });
    }

    @Override
    protected void onRightTextClick() {
        super.onRightTextClick();
        Intent i = new Intent(this, TXMXActivity.class);
        startActivity(i);
    }
}
